package com.sinosoft.validateCode.util;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.*;

/**
 * @author handong
 */
public class ImageUtil {


    /***
     * 截取图片
     * @param image 图片
     * @param range 截取范围
     * @return
     * @throws IOException
     */
    public static BufferedImage  cutImage(File image,CutRange range) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(image);
        return   cutImage(bufferedImage,range);
    }

    /**
     * 截图
     * @param image
     * @param range
     * @return
     */
    public static  BufferedImage cutImage(BufferedImage image, CutRange range) throws IOException {
        BufferedImage bufferedImage = image.getSubimage(range.getX(),range.getY(),range.getWidth(),range.getHeight());
        return  bufferedImage;
    }



    /***
     * 随机截取图片
     * @param image 图片
     * @param width 宽度
     * @param height 高度
     * @return 数据流
     * @throws IOException
     */
    public static BufferedImage randomCutImage(File image, int width , int height) throws IOException {
        BufferedImage bi = ImageIO.read(image);
        int image_width=  bi.getWidth();
        int image_height =  bi.getHeight();
        int image_cut_y = image_height - height;
        int image_cut_x = image_width - width;
        int  x = (int) (Math.random() * image_cut_x);
        int y = (int) (Math.random() * image_cut_y);
        CutRange range = new CutRange(x,y,width,height);
        return   cutImage(image , range);
    }




    public static void inputStreamToFile(InputStream ins,File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /***
     * 截图
     * @param bufferedImage 图片流
     * @return 写入流
     * @throws IOException
     */
    public static  InputStream imageToInputStream(BufferedImage bufferedImage) throws IOException {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
        ImageIO.write(bufferedImage, "jpg", imOut);
        InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
        return  inputStream;
    }




    public static void main(String[] args) throws IOException {




    }


    /***
     * 截出移动图片
     * @param cutRange
     * @param bufferedImage
     */
    public static BufferedImage createMoveImage(CutRange cutRange,BufferedImage bufferedImage) throws IOException {
        BufferedImage moveImage =  cutImage(bufferedImage,cutRange);
        return  moveImage;
    }



    public static BufferedImage deepCopy(BufferedImage bi) {
        ColorModel cm = bi.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = bi.copyData(null);
        return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
    }

    private  static int[][]  domeData() throws IOException {

        File file = new File("/Users/handong/Desktop/1207491-20170913105058469-2127815569.png");

        BufferedImage image = ImageIO.read(file);

        for (int x = 0; x < image.getWidth(); x++) {

            for (int y = 0; y < image.getHeight(); y++) {

                int b = image.getRGB(x, y);

                if (b !=0){
                    b=1;
                }

                System.out.print(b);

            }

            System.out.println();
        }


        return null;
    }







}
